<?php

/**
 * 会员控制器
 */
namespace Custom\Controller;
use Think\Controller;
class MemberController extends UserController {

    function __construct()
    {
        parent::__construct();
        $this->M_user = M('user');
    }

    /**
     *  会员列表
     */
    public function index(){
        if($this->s_id){
            $where = 's_id = '.$this->s_id;
            if($_GET['nickname']){
                $where .= ' AND nickname LIKE\'%'.$_GET['nickname'].'%\'';
                $this->assign('nickname',$_GET['nickname']);
            }
            if($_GET['tel']){
                $where .= ' AND tel LIKE\'%'.$_GET['tel'].'%\'';
                $this->assign('tel',$_GET['tel']);
            }
            if($_GET['start_time']){
                $start_time = strtotime($_GET['start_time']);
                $where .= ' AND register_time > '.$start_time;
                $this->assign('start_time',$_GET['start_time']);
            }
            if($_GET['end_time']){
                $end_time = strtotime($_GET['end_time'])+86400;
                $where .= ' AND register_time < '.$end_time;
                $this->assign('end_time',$_GET['end_time']);
            }
            $count = $this->M_user->where($where)->order('register_time desc')->count();
            $page_num = $this->is_page($count);
            $member_list = $this->M_user->where($where)->order('register_time desc')->limit($page_num->firstRow.','.$page_num->listRows)->select();
            $this->assign("count",$count);
            $this->assign('list',$member_list);
        }
        $this->display();
    }

    /**
     *  修改会员
     */
    public function edit(){
        $id = (int)$_GET['id'];
        if(IS_POST){
            $this->M_user->nickname = $_POST['nickname'];
            $this->M_user->full_name = $_POST['full_name'];
            $upload = new \Think\Upload();
            $upload->maxSize = 3145728 ;
            $upload->exts = array('jpg', 'gif', 'png', 'jpeg');
            $upload->rootPath = './';
            $upload->savePath = './Uploads/'.$this->c_id.'/member/'.$this->uid.'/';
            $img_info = $upload->upload();
            if($img_info) {
                $headimgurl = $img_info['headimgurl']['savepath'].$img_info['headimgurl']['savename'];
                $this->M_user->headimgurl = $headimgurl;
            }
            if($_POST['password']) $this->M_user->password = md5($_POST['password']);
            if($id) $result = $this->M_user->where(array('user_id'=>$id))->save();
            if($result)$this->success('L("adsuccess")',__MODULE__.'/Member');
            else $this->error('L("failed")');
        }else{
            if(!$id) die($this->error('L("failed")'));
            $info = $this->M_user->find($id);
//            $temp = $this->M_user->find($info['tuijian_id']);
//            $info['tuijian'] = $temp['nickname'];
            $this->assign('info',$info);
        }
        $this->display();
    }

    /**
     *  删除单一会员
     */
    public function delete(){
        $where['user_id'] = (int)$_GET['id'];
        $where['c_id'] = $this->c_id;
        $info = $this->M_user->find($where['user_id']);
        if($info){
            $result = $this->M_user->where($where)->delete();
            if ($result) $this->ajaxReturn(array('result'=>'yes'));
            else $this->ajaxReturn(array('result'=>'no'));
        }
        else $this->ajaxReturn(array('result'=>'no'));
    }

}